package com.redpack.redpost.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.util.ArrayList;
import java.util.List;

import com.redpack.redpost.util.dao.UserDAO;
import com.redpack.redpost.util.dao.adminDAO;
import com.redpack.redpost.util.to.UserLogin;
import com.redpack.redpost.util.to.UserTo;
import com.redpack.redpost.util.to.generalUtil;
import com.redpack.redpost.util.to.validaciones;

public class puntos extends Action {
	
	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
		HttpSession session = request.getSession( ) ;
		
		String salida = "success",
				idioma = "";
		
		if ( session.getAttribute("idioma") != null ){
			idioma = session.getAttribute("idioma").toString();
		}
		
		salida = validaciones.idioma(salida, idioma) ;
		
		UserLogin login = null ;
	    String nombre = "",
				apellido = "" ;

	    if( generalUtil.isLoggedIn( request ) ){
	    	
	    	login = (UserLogin) session.getAttribute("UserLogin") ;
	    	nombre = login.getNombre( ) ;
	    	apellido = login.getApellido( ) ;
	    	request.setAttribute("Nombre", nombre) ;
	    	request.setAttribute("Apellido", apellido) ;
	    }else{
	    	return mapping.findForward("noLogin");
	    }
	    
	    int arribos = 0 ;
	    
	    try{
	    	
	    	UserTo to = UserDAO.getCompleteUser( login.getUser( ) ) ;
	    	
	    	ArrayList arribosArray = UserDAO.arribos( to.getNip( ) ) ;
	    	
	    	arribos = arribosArray.size( ) ;
	    	
	    }catch( Exception e ){
	    	
	    	e.printStackTrace( ) ;
	    	
	    }
	    		
	    request.setAttribute("User", login) ;
	    request.setAttribute("Arribos", "" + arribos) ;
		
	    return mapping.findForward(salida);
	    
	    }

}
